package com.future.mayi.qsale.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.future.mayi.R;
import com.future.mayi.model.interfaces.OnItemClickListener;
import com.future.mayi.qsale.adapter.SelectImgAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Created by Administrator on 2018/6/19.
 */

public class SelectImgView extends LinearLayout {

    private Context mContext;
    private RecyclerView recyclerView;
    private int maxSelectNum = 12;

    private SelectImgAdapter selectImgAdapter;

    public SelectImgView(Context context) {
        this(context, null);
    }

    public SelectImgView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SelectImgView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;

        View.inflate(context, R.layout.layout_select_img, this);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view_img);

        List<String> list = new ArrayList<>(maxSelectNum);
        list.add(SelectImgAdapter.ADD_ITEM_STRING);

        selectImgAdapter = new SelectImgAdapter(context, list);

        GridLayoutManager mgr = new GridLayoutManager(mContext, 4);
        recyclerView.setLayoutManager(mgr);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setHasFixedSize(false);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(selectImgAdapter);
    }

    public void addImg(List<String> path) {
        if (selectImgAdapter == null)
            return;

        if (path == null)
            return;


        if (selectImgAdapter.getItemCount() > 0) {
            selectImgAdapter.delete(selectImgAdapter.getItemCount() - 1);
        }


        if (path.size() != maxSelectNum && (selectImgAdapter.getItemCount() + path.size()) < maxSelectNum)
            path.add(SelectImgAdapter.ADD_ITEM_STRING);


        if (selectImgAdapter.getList().size() == maxSelectNum) {
            selectImgAdapter.setList(path);
            selectImgAdapter.notifyDataSetChanged();
        } else {
            selectImgAdapter.addMore(path);
        }

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        selectImgAdapter.setOnItemClickListener(onItemClickListener);
    }

    public void setImgList(List<String> imgList) {
        if (selectImgAdapter != null) {
            if (imgList == null || imgList.size() == 0) {
                imgList = new ArrayList<>();
                imgList.add(SelectImgAdapter.ADD_ITEM_STRING);
            }
            selectImgAdapter.setList(imgList);
        }
    }

    public boolean contentIsEmpty() {
        if (selectImgAdapter != null && selectImgAdapter.getItemCount() > 0) {
            if (!selectImgAdapter.getList().get(0).equals(SelectImgAdapter.ADD_ITEM_STRING)) {
                return false;
            }
        }
        return true;
    }

    public List<String> getImg() {
        if (contentIsEmpty())
            return null;
        List<String> list = new ArrayList<>();
        for (int i = 0; i < selectImgAdapter.getList().size(); i++) {
            if (!selectImgAdapter.getList().get(i).equals(SelectImgAdapter.ADD_ITEM_STRING)) {
                String path = selectImgAdapter.getList().get(i);
                if (path != null)
                    list.add(path);
                if (list.size() == maxSelectNum)
                    break;
            }
        }
        return list;
    }

    public int getMaxSelectNum() {
        List<String> list = getImg();
        int size = maxSelectNum;
        if (list != null)
            size = size - list.size();
        if (size == 0)
            size = maxSelectNum;
        return size;
    }

}
